package com.example.arithmeticleetcode.leetcode.nov;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-20 09:36
 **/
public class Demo13 {

    public static void main(String[] args) {
        int[] groupSizes1 = {3,3,3,3,3,1,3};
        int[] groupSizes2 = {2,1,3,3,3,2};
        System.out.println(groupThePeople1(groupSizes1));
        System.out.println(groupThePeople1(groupSizes2));
    }

    public static List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> result = new ArrayList<>();
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < groupSizes.length; i++) {
            if (map.containsKey(groupSizes[i])) {
                List<Integer> list = map.get(groupSizes[i]);
                list.add(i);
                map.put(groupSizes[i], list);
            } else {
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(groupSizes[i], list);
            }
        }

        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
            Integer key = entry.getKey();
            List<Integer> value = entry.getValue();
            int count = 0;
            int size = value.size();
            while (count < size) {
                int temp = key;
                List<Integer> list = new ArrayList<>();
                while (temp-- > 0) {
                    list.add(value.get(count++));
                }
                result.add(list);
            }
        }
        return result;
    }

    public static List<List<Integer>> groupThePeople1(int[] groupSizes) {
        List<List<Integer>> list = new ArrayList();
        List<List<Integer>> res = new ArrayList();
        int len = groupSizes.length;

        for(int i = 0; i <= len; i++) {
            list.add(new ArrayList<>());
        }

        for(int i = 0; i < len; i++) {
            list.get(groupSizes[i]).add(i);
        }

        for(int i = 1; i <= len; i++) {
            for(int j = 0, len1 = list.get(i).size(); j < len1; j += i) {
                List<Integer> tmp = new ArrayList<>();
                for(int k = j; k < j + i; k++) {
                    tmp.add(list.get(i).get(k));
                }
                res.add(tmp);
            }
        }

        return res;
    }
}
